package com.fnzn.entranceGuard.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fnzn.entranceGuard.entity.GuardDevice;
import com.fnzn.entranceGuard.entity.power.PowerDevice;
import com.fnzn.entranceGuard.mapper.GuardDeviceMapper;
import com.fnzn.entranceGuard.mapper.GuardIcInfoMapper;
import com.fnzn.entranceGuard.service.IGuardDeviceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fnzn.entranceGuard.util.tree.TreeUtil;
import com.fnzn.entranceGuard.vo.CommunityVo;
import com.fnzn.entranceGuard.vo.DeviceTree;
import com.github.pig.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxf
 * @since 2018-06-25
 */
@Service
public class GuardDeviceServiceImpl extends ServiceImpl<GuardDeviceMapper, GuardDevice> implements IGuardDeviceService {

    @Autowired
    GuardDeviceMapper deviceMapper;
    @Autowired
    GuardIcInfoMapper guardIcInfoMapper;

    @Override
    public List<DeviceTree> listTree(String userName) {
        //根据社区获取所属设备
        List<DeviceTree> devs = new ArrayList<>();
        //根据所在部门获取社区列表
        List<CommunityVo> cList = new ArrayList<>();
        if(CommonConstant.MANAGE_NAME.equals(userName)){
            devs.addAll(deviceMapper.listTree(null));
        }else{
            cList = deviceMapper.getCommunitysByCurrentUser(userName);
            cList.forEach(cv ->{
                devs.addAll(deviceMapper.listTree(cv.getDeptId().toString()));
            });
        }
        List<DeviceTree> tree = TreeUtil.buildByRecursive(devs,"0");
        return tree;
    }

    @Override
    public Integer getMaxSortByCommunityId(String communityId) {
        return deviceMapper.getMaxSortByCommunityId(communityId);
    }

    @Override
    public Integer getCommunityCodeByDeviceId(String deviceId) {
        return deviceMapper.getCommunityCodeByDeviceId(deviceId);
    }

    @Override
    public GuardDevice getDeviceCodeByDeviceId(String deviceId) {
        List<GuardDevice> d  = deviceMapper.selectByDeviceId(deviceId);
         if(d!=null && d.size()>0){
             return d.get(0);
         }
        return null;
    }

    /**
     * 根据企业和类型查询设备
     * @param companyId
     * @param type 电表or门禁
     * @return
     */
    @Override
    public GuardDevice getByType(Integer companyId,String type) {
        Wrapper wrappr = new EntityWrapper();
        wrappr.eq(CommonConstant.DEL_FLAG,CommonConstant.STATUS_NORMAL);
        wrappr.eq("community_id",companyId);
        wrappr.eq("type",type);
        List<GuardDevice> list = deviceMapper.selectList(wrappr);
        if(list!=null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据企业查询所有的电表
     * @param companyId
     * @return
     */
    @Override
    public List<PowerDevice> getPowerDeviceByCompany(Integer companyId) {
        return deviceMapper.getPowerDeviceByCompany(companyId);
    }

}
